package com.buyhouse.zhaimao.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.model.CommunityBean;
import com.buyhouse.zhaimao.model.NearByExpertBean;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NearbyExpertAdapter extends BaseAdapter{

	private Context context;
	private List<NearByExpertBean> listNearByExpertBean;
	private LayoutInflater inflater;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options, headOptions;
	
	public NearbyExpertAdapter(Context context, List<NearByExpertBean> listNearByExpertBean){
		this.context = context;
		this.listNearByExpertBean = listNearByExpertBean;
		this.inflater=LayoutInflater.from(context);
		initImgLoader(context);
	}
	
	public ImageLoader getImageLoader() {
		return imageLoader;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listNearByExpertBean.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listNearByExpertBean.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_nearbyexper, null);
			holder = new ViewHolder();
			holder.mynews_tv = (TextView) convertView
					.findViewById(R.id.mynews_tv);
			holder.mynews_flag = (TextView) convertView
					.findViewById(R.id.mynews_flag);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
		String nowDate=format.format(new Date());
		String[] serviceDate=listNearByExpertBean.get(position).getSendTime().split(" ");
		
		int sdateintNum=Integer.parseInt(serviceDate[0].replace("-", ""));
		int nowdate=Integer.parseInt(nowDate);
		if(nowdate>sdateintNum){
			holder.mynews_flag.setText(serviceDate[0]);
		}else{
			holder.mynews_flag.setText("werwert");//serviceDate[1]
		}
		
//		holder.mynews_tv.setText(TextUtilForStr.replace(listNearByExpertBean.get(position).getContent()));
//		holder.mynews_flag.setText(list.get(position).getSendTime());

		return convertView;
	}

	static class ViewHolder {
		TextView mynews_tv, mynews_flag;
	}
	private void initImgLoader(Context context) {
		imageLoader
				.init(new ImageLoaderConfiguration.Builder(context)
						.threadPriority(Thread.NORM_PRIORITY - 2)
						.threadPoolSize(3)
						.denyCacheImageMultipleSizesInMemory()
						.memoryCache(new WeakMemoryCache())
						.diskCacheFileNameGenerator(new Md5FileNameGenerator())
						.tasksProcessingOrder(QueueProcessingType.LIFO)
						.denyCacheImageMultipleSizesInMemory().writeDebugLogs()
						.build());
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.imageScaleType(ImageScaleType.EXACTLY)
				// 图像将完全按比例缩小的目标大小
				.cacheOnDisk(true)
				// 设置下载的图片是否缓存在SD卡中
				.showImageForEmptyUri(R.drawable.ic_home)
				.showImageOnFail(R.drawable.ic_home)
				.showImageOnLoading(R.drawable.ic_home)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		


	}
	
}
